package com.ly.algorithm.DataStructures.stack;

import java.util.Stack;

/**
 * @Classname Calculator
 * @Description TODO
 * @Date 2020/4/22 18:34
 * @Author 冷心影翼
 */
public class Calculator {

    private Stack<String> operatorStack = new Stack<>();

    private Stack<Integer> numberStack = new Stack<>();

    public boolean isOperator(String s) {
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
            return true;
        }else {
            return false;
        }
    }

    public int cal(int num1,int num2,String op) {
        if (op.equals("+")) {
            return num1+num2;
        }else if(op.equals("-")) {
            return num2-num1;
        }else if(op.equals("*")) {
            return num1 * num2;
        }else {
            return num2/num1;
        }
    }

    public int quantity(String c) {
        if(c.equals("*") || c.equals("/")) {
            return 2;
        }else if(c.equals("+") || c.equals("-")) {
            return 1;
        }else {
            return 0;
        }
    }

    public void parseExpression(String expression) {
        String op = "";
        int num1 = 0;
        int num2 = 0;
        int res = 0;
        int index = 0;
        String s =  "";
        while (true) {
            s = expression.substring(index, index + 1);
            //判断是否为操作符，是数字直接入栈
            if (isOperator(s)) {
                //操作符栈为空，直接入栈
                if(operatorStack.isEmpty()) {
                    operatorStack.push(s);
                }else {
                    //当然优先级 高于 操作栈 入栈
                    if(quantity(s) > quantity(operatorStack.peek())) {
                        operatorStack.push(s);
                    }else {
                        //否则 弹出 两个数字， 弹出一个操作符 运算，结果压入栈中， 当然操作符压入
                        num1 = numberStack.pop();
                        num2 = numberStack.pop();
                        op = operatorStack.pop();
                        res = cal(num1,num2,op);
                        numberStack.push(res);
                        operatorStack.push(s);
                    }
                }
            } else {
                //判断是否为多位数字
                while(true) {
                    index++;
                    if (index >= expression.length() || isOperator(expression.substring(index, index + 1)) ) {
                        break;
                    }else {
                        s+= expression.substring(index, index + 1);
                    }
                }
                //多移了一位
                index--;
                numberStack.push(Integer.parseInt(s));
                s = "";
            }
            //后移
            index++;
            if(index >= expression.length()) {
                break;
            }
        }

        while (true) {
            if(operatorStack.isEmpty()) {
                break;
            }else {
                num1 = numberStack.pop();
                num2 = numberStack.pop();
                op = operatorStack.pop();
                res = cal(num1,num2,op);
                numberStack.push(res);
            }
        }

        System.out.println(numberStack.peek());
    }
}

class Test3 {

    public static void main(String[] args) {
        String excepess = "7-6-1.txt-8";
        Calculator calc = new Calculator();
        calc.parseExpression(excepess);
    }
}
